package com.web_bloks.project.pages.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import com.web_bloks.content.client.BasePage;
import com.web_bloks.content.client.ViewPage;
//import com.web_bloks.system.client.Logger;
import com.web_bloks.user.client.StatusMessageServer;
import com.web_bloks.user.client.User;
import com.web_bloks.user.client.UserManager;
import com.web_bloks.user.client.UserMessageServer;
import com.web_bloks.widget.client.GearsStatus;
import com.web_bloks.widget.client.ImgAnchor;
import com.web_bloks.widget.client.LoginLogoutWidget;
import com.web_bloks.widget.client.LogoWidget;
import com.web_bloks.widget.client.StatusWidget;
import com.web_bloks.widget.client.UserMessageWidget;
import com.web_bloks.system.client.Logger;

/**
 * 
 * @author Jean-Lou Dupont
 *
 */
public class Base extends Composite 
	implements BasePage {
	
	final static PageMessages MSG = (PageMessages) GWT.create(PageMessages.class);

	MainPage mp = null;
	
	ViewPage currentView = null;
	Widget currentWidget = null;
	
	public VerticalPanel verticalPanel = null;
	public HorizontalPanel horizontalPanel = null;
	public SimplePanel contentWrapper = null;
	
	public FlexTable flexTableHeader = null;
	public FlexTable flexTableFooter = null;
	
	public Label webbloksTitle = new Label();
	
	// Frame Widget
	public LoginLogoutWidget loginLogout = null;
	public GearsStatus googleGears = null;
	public StatusWidget statusIcon; 	

	// Buttons
	
	// User Message
	UserMessageWidget userMessageWidget = null;
	
	// Logo
	LogoWidget logo = new LogoWidget();
	
	public ImgAnchor imgAnchorLink = null;
	
	Button clearGearsCacheButton;
	
	protected Base() {
		super();
		setup();
	}

	protected void setup() {
		Logger.logInfo(this.getClass()+".setup");
		
		verticalPanel = new VerticalPanel();
		initWidget( verticalPanel );		

		setSize("100%", "100%");
		
		horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		verticalPanel.setSize("100%", "100%");

		flexTableHeader = new FlexTable();

		contentWrapper = new SimplePanel();
		verticalPanel.add(contentWrapper);
		verticalPanel.setCellVerticalAlignment(contentWrapper, HasVerticalAlignment.ALIGN_TOP);
		verticalPanel.setCellHorizontalAlignment(contentWrapper, HasHorizontalAlignment.ALIGN_LEFT);
		contentWrapper.setTitle("");
		contentWrapper.setSize("100%", "100%");
		verticalPanel.setCellHeight(contentWrapper, "100%");
		verticalPanel.setCellWidth(contentWrapper, "100%");
		
		flexTableFooter = new FlexTable();
		
		webbloksTitle = new Label();		
		
		loginLogout = new LoginLogoutWidget();
		googleGears = new GearsStatus();

// ===============================================================
		

		verticalPanel.setCellVerticalAlignment(horizontalPanel, HasVerticalAlignment.ALIGN_TOP);
		horizontalPanel.setSize("100%", "32");
		verticalPanel.setCellHeight(horizontalPanel, "32");
		verticalPanel.setCellWidth(horizontalPanel, "100%");
		horizontalPanel.setStylePrimaryName("gwt-TopPanel");
		horizontalPanel.setStyleName("gwt-TopPanel");


		horizontalPanel.add(flexTableHeader);
		flexTableHeader.setStyleName("gwt-Header");
		horizontalPanel.setCellWidth(flexTableHeader, "100%");
		flexTableHeader.setWidth("100%");


		flexTableHeader.setWidget(0, 1, webbloksTitle);
		webbloksTitle.setWidth("120px");
		flexTableHeader.getCellFormatter().setWidth(0, 1, "100px");
		webbloksTitle.setStylePrimaryName("gwt-Title");
		webbloksTitle.setStyleName("gwt-Title");
		webbloksTitle.setText("Web-Bloks");
		flexTableHeader.getCellFormatter().setWidth(0, 1, "100%");

		// Login/Logout
		flexTableHeader.setWidget(0, 3, loginLogout);
		flexTableHeader.getCellFormatter().setWidth(0, 3, "35");
		flexTableHeader.getCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_RIGHT);
		
		//imgAnchorLink = new ImgAnchor();
		flexTableHeader.setWidget(0, 0, logo);
		flexTableHeader.getCellFormatter().setWidth(0, 0, "50px");
		logo.setHref("#Main");
		//imgAnchorLink.setImgUrl("logo_32x32.png");
	

		// Message area
		flexTableHeader.getCellFormatter().setWidth(0, 1, "380px");

		userMessageWidget = new UserMessageWidget("<hr>");
		flexTableHeader.setWidget(0, 2, userMessageWidget);
		flexTableHeader.getCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
		flexTableHeader.getCellFormatter().setWidth(0, 2, "100%");
		userMessageWidget.setText("");

		verticalPanel.add(flexTableFooter);
		flexTableFooter.setStyleName("gwt-Footer");
		verticalPanel.setCellHorizontalAlignment(flexTableFooter, HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.setCellHeight(flexTableFooter, "100%");
		verticalPanel.setCellWidth(flexTableFooter, "100%");
		flexTableFooter.setSize("100%", "100%");

		// Content region

		flexTableFooter.setWidget(1, 0, googleGears);
		flexTableFooter.getCellFormatter().setHorizontalAlignment(1, 4, HasHorizontalAlignment.ALIGN_RIGHT);
		flexTableFooter.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		flexTableFooter.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_RIGHT);

		final Label labelFooterMessage = new Label("");
		flexTableFooter.setWidget(1, 1, labelFooterMessage);
		flexTableFooter.getCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
		labelFooterMessage.setVisible(false);
		flexTableFooter.getCellFormatter().setWidth(1, 1, "100%");
		labelFooterMessage.setWidth("100%");

		statusIcon = new StatusWidget();
		flexTableFooter.setWidget(1, 3, statusIcon);

		final HTML html = new HTML("<hr>");
		flexTableFooter.setWidget(0, 0, html);
		flexTableFooter.getFlexCellFormatter().setRowSpan(0, 0, 4);

		clearGearsCacheButton = new Button();
		flexTableFooter.setWidget(1, 2, clearGearsCacheButton);
		clearGearsCacheButton.setVisible(false);
		clearGearsCacheButton.setText("Clear Gears Cache");
		
		clearGearsCacheButton.addClickListener( new ClickListener() {

			public void onClick(Widget sender) {
				
			}
			
		});
		
		//setupLoginLogout();
	}
	
	public void setupTools() {
		
		//UserManager
		UserManager um = GWT.create(UserManager.class);
		
		// init current user
		User user = um.getCurrentUser();
		
		if (null==user) {
			throw new RuntimeException(this.getClass()+".initUser: user is null");
		}
		
		String login_url  = user.getParam("login_url");
		String logout_url = user.getParam("logout_url");

		this.loginLogout.setLoginUrl(login_url);
		this.loginLogout.setLogoutUrl(logout_url);
		
		String username = user.getParam("name");
		
		// if username is not empty, then we are logged-in
		if (username != null && username.length() != 0) {
			user.setLoginState( true );
			this.loginLogout.setState(LoginLogoutWidget.State.logout);
		} else {
			user.setLoginState( false );
			this.loginLogout.setState(LoginLogoutWidget.State.login);
		}
		
		clearGearsCacheButton.setVisible( user.isAdmin() );
	}
	/**
	 * @see BasePage
	 */
	public boolean isCurrentView(String viewname) {
		if (null!=currentView) {
			return currentView.getViewName() == viewname ;
		}
		return false;
	}
	/**
	 * @see BasePage
	 */
	public void onBeforeChangingBody() {
	}
	/**
	 * @see BasePage
	 */
	public void setBody(ViewPage cp) {
		Logger.logDebug(this.getClass()+".setBody: BEGIN, cp name="+cp.getPageName());
		
		currentView = cp;
		currentWidget = cp.getViewWidget();
		contentWrapper.setWidget( currentWidget );
		
		//Logger.logDebug(this.getClass()+".setBody: END");
	}
	
	public UserMessageWidget getUserMessageWidget() {
		return userMessageWidget;
	}

	public StatusMessageServer getStatusMessageServer() {
		return statusIcon;
	}
	
	public UserMessageServer getUserMessageServer() {
		return userMessageWidget;
	}

	public void doUpdate() {
		if (null!=currentView) {
			currentView.doUpdate();
		}
	}
}//END
